import { sources, blogTypes } from 'common/utils/classify'
import { getArticleList, getAsideList, getThisArticle, getThisTag, getThisSort, addThumbsUp, getArticleAroundThis } from 'api/article'

const article = {
  state: {
    sources,
    blogTypes,
    pageindex: 1,
    pagesize: 10,
    thisArticle: {},
    commentList: {},
    articleList: [],
    articleTotal: 0,
    category: []
  },
  mutations: {
    SET_ARTICLELIST(state, data) {
      state.articleList.push(...data.list)
      state.articleTotal = data.total
    },
    SET_THISARTICLE(state, data) {
      state.thisArticle = data
      // state.category = data.toc
    },
    SET_CATEGORY(state, category) {
      state.category = category
    },
    SET_PAGEINDEX(state, index) {
      state.pageindex = index
    }
  },
  actions: {
    // 获取文章列表
    GetArticleList({ commit, state }, params) {
      if (params.type !== 'search') {
        commit('SET_PAGEINDEX', params.pageindex)
      }
      return new Promise((resolve, reject) => {
        getArticleList(params).then(response => {
          if (params.type !== 'search') {
            if (params.all === 'all') {
              state.articleList = []
            }
            commit('SET_ARTICLELIST', response.data)
          }
          resolve(response.data)
        }).catch(err => {
          reject(err)
        })
      })
    },
    // 获取某篇上一篇下一篇文章
    GetAsideList({ commit }) {
      return new Promise((resolve, reject) => {
        getAsideList().then(response => {
          resolve(response.data)
        }).catch(err => {
          reject(err)
        })
      })
    },
    GetThisTagList({ commit }, params) {
      return new Promise((resolve, reject) => {
        getThisTag(params).then(response => {
          resolve(response.data)
        }).catch(err => {
          reject(err)
        })
      })
    },
    GetThisSortList({ commit }, params) {
      return new Promise((resolve, reject) => {
        getThisSort(params).then(response => {
          resolve(response.data)
        }).catch(err => {
          reject(err)
        })
      })
    },
    // 获取某篇文章
    GetThisArticle({ commit }, id) {
      return new Promise((resolve, reject) => {
        getThisArticle(id).then(response => {
          commit('SET_THISARTICLE', response.data)
          resolve(response.data)
        }).catch(err => {
          reject(err)
        })
      })
    },
    // 获取某篇上一篇下一篇文章
    GetArticleAroundThis({ commit }, id) {
      return new Promise((resolve, reject) => {
        getArticleAroundThis(id).then(response => {
          resolve(response.data)
        }).catch(err => {
          reject(err)
        })
      })
    },
    // 点赞
    AddThumbsUp({ commit }, info) {
      return new Promise((resolve, reject) => {
        addThumbsUp(info).then(response => {
          resolve(response)
        }).catch(error => {
          reject(error)
        })
      })
    },
    CreateCategory({ commit }, category) {
      return new Promise((resolve, reject) => {
        commit('SET_CATEGORY', category)
        resolve(category)
      })
    }
  }
}
export default article
